package com.masterclient.app.businessmodule.homemodule.view.adapter.recommand;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.businessmodule.homemodule.bean.shoplist.ProductHomeMessage;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.footertab.HomeFooterTabBean;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import java.util.ArrayList;
import java.util.List;


public class HomePageNewXFootContentAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private HomePageWrapContentHeightViewPager viewPager;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private List<View> mPagerList = new ArrayList<>();//页面集合
    private List<HomeFooterTabBean>tabBeanList;
    private List<HomePageFootRecycleHelper>recycleHelperList = new ArrayList<>();

    public HomePageNewXFootContentAdapter(Context context, LayoutHelper layoutHelper,List<HomeFooterTabBean>tabBeanList, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.tabBeanList = tabBeanList;
        this.mLayoutParams = layoutParams;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }


    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }
        viewPager = holder.itemView.findViewById(R.id.hot_view_pager);
        if (tabBeanList!=null && tabBeanList.size() > 0) {
            viewPager.setVisibility(View.VISIBLE);
            viewPager.setOffscreenPageLimit(tabBeanList.size());
            for (int i = 0; i < tabBeanList.size(); i++) {
                HomeFooterTabBean tabBean = tabBeanList.get(i);
                HomePageFootRecycleHelper recycleHelper = new HomePageFootRecycleHelper(mContext,tabBean.getTitle());
                viewPager.setpositionAndView(i,recycleHelper.getHotRecyclerView());
                mPagerList.add(recycleHelper.getHotRecyclerView());
                recycleHelperList.add(recycleHelper);
            }
            viewPager.setAdapter(new HomePageViewPagerAdapter(mPagerList,mContext));
        } else {
            viewPager.setVisibility(View.GONE);
        }
    }

    public void refreshData(int index, List<ProductHomeMessage>productMessageList) {
        if (viewPager != null){
            viewPager.setCurrentItem(index,false);
            viewPager.resetHeight(index);
            if (recycleHelperList!=null && recycleHelperList.size() > 0 && productMessageList!=null) {
                recycleHelperList.get(index).addData(productMessageList);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 6;
    }
}
